package com.hqyj.hrms.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.account.dao.UserMapper;
import com.hqyj.hrms.modules.account.entity.User;
import com.hqyj.hrms.modules.account.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {
    //注入mapper
    @Autowired(required = false)
    UserMapper userMapper;

    //登录
    @Override
    public HashMap<String, Object> login(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        User user1 = userMapper.login(user);
        if (user1 == null) {
            map.put("info", "登录失败");
        } else {
            map.put("info", "登录成功");
            map.put("info1", user1.getUserId());
            System.out.println(user1.getUserId());
        }
        return map;
    }

    //注册
    @Override
    public HashMap<String, Object> register(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int user2 = userMapper.insert(user);
        if (user2 == 0) {
            map.put("info", "注册失败");
        } else {
            map.put("info", "注册成功");
        }
        return map;
    }

    //退出
    @Override
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        Session session = subject.getSession();
        session.removeAttribute("userId");
    }

    //新增
    @Override
    public HashMap<String, Object> add(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = userMapper.insert(user);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    //修改
    @Override
    public HashMap<String, Object> update(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = userMapper.updateById(user);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "修改失败");
        }
        return map;
    }

    //删除
    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = userMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    //查询
    @Override
    public HashMap<String, Object> list(User user) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //第一步：创建分页对象
        Page<User> userInfoPage = new Page<>(user.getPage(), user.getRow());
        //创建查询条件对象
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                user.getUserName() != null && !user.getUserName().equals(""),
                "user_name",
                user.getUserName());

        //第二步：查询分页，获取分页数据集
        Page<User> pageList = userMapper.selectPage(userInfoPage, queryWrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public User getUserByUserName(String userName) {
        return userMapper.getUserByUserName(userName);
    }
}
